﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Nuzzle.Utilities
{
    public class Basic
    {
        public static Random Randomizer = new Random();
        public static string[] Hash = { "a", "z", "e", "r", "t", "y", "u", "i", "o", "p", "q", "s", "d",
                                          "f", "g", "h", "j", "k", "l", "m", "w", "x", "c", "v", "b", "n" };

        /// <summary>
        /// Get a random string
        /// </summary>
        /// <param name="lenght">Lenght of string</param>
        /// <returns>Random String</returns>
        public static string RandomString(int lenght)
        {
            string str = string.Empty;
            for (int i = 1; i <= lenght; i++)
            {
                int randomInt = Randomizer.Next(0, Hash.Length);
                str += Hash[randomInt];
            }
            return str;
        }

        public static string FirstLetterToUpper(string str)
        {
            return char.ToUpper(str[0]) + str.Substring(1);
        }

        /// <summary>
        ///   Convert html chars to HTML entities
        /// </summary>
        /// <param name = "str"></param>
        /// <returns></returns>
        public static string HtmlEntities(string str)
        {
            str = str.Replace("&", "&amp;");
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            return str;
        }

        public static string MD5EncodePassword(string originalPassword)
        {
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            // Conver the original password to bytes; then create the hash
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            // Bytes to string
            return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(encodedBytes), "-", "").ToLower();
        }

        public static string ReplaceBySettingsVar(string str)
        {
            return str.Replace("{nuzzle_version}", GetNuzzleVersion().ToString());
        }

        public static Version GetNuzzleVersion()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
        }
    }
}
